package com.zhengsr.wanandroid.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zhengsr.corelib.bean.CollectBean;
import com.zhengsr.corelib.bean.NaviData;
import com.zhengsr.corelib.bean.NaviSection;
import com.zhengsr.corelib.utils.Lgger;
import com.zhengsr.wanandroid.R;
import com.zhengsr.wanandroid.mvp.base.BaseFragment;
import com.zhengsr.wanandroid.mvp.base.BasePresent;
import com.zhengsr.wanandroid.mvp.contract.ContractManager;
import com.zhengsr.wanandroid.mvp.present.NaviPresent;
import com.zhengsr.wanandroid.ui.main.adapter.NaviAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by zhengshaorui
 * Time on 2019/3/4
 */

public class NaviFragment extends BaseFragment implements ContractManager.InaviView, BaseQuickAdapter.OnItemClickListener {

    @BindView(R.id.navi_recycler)
    RecyclerView mRecyclerView;
    @BindView(R.id.navi_left_recycler)
    RecyclerView mSortRecyclerView;


    private List<DetailGoods> mNaviData = new ArrayList<>();
    private List<String> mTitles = new ArrayList<>();
    private NaviAdapter mNaviAdapter;
    private SortAdapter mSortAdapter;
    private int mCurrentIndex = 0;
    private GridLayoutManager mManager;

    public static NaviFragment newInstance() {

        Bundle args = new Bundle();

        NaviFragment fragment = new NaviFragment();
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public int getLayoutId() {
        return R.layout.navi_fragment;
    }

    @Override
    public BasePresent getPresent() {
        return NaviPresent.create(this);
    }

    @Override
    public void initView(View view) {
       // mManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        mManager = new GridLayoutManager(_mActivity,2);
        mRecyclerView.setLayoutManager(mManager);
        List<NaviSection> datas = new ArrayList<>();
        mNaviAdapter = new NaviAdapter(R.layout.item_text_navi,R.layout.item_text_header,datas);
        mRecyclerView.setAdapter(mNaviAdapter);


        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                //滚动结束
                if (newState == RecyclerView.SCROLL_STATE_IDLE){
                    int firstIndex = mManager.findFirstVisibleItemPosition();
                    int lastIndex = mManager.findLastVisibleItemPosition();
                    NaviSection section = mNaviAdapter.getItem(firstIndex);
                    int index = 0;
                    if (section != null) {
                       if (section.t != null){
                          index = section.t.getParentIndex();
                       }else{
                          index = section.getPosition();
                       }
                    }
                    mCurrentIndex = index;
                    mSortAdapter.notifyDataSetChanged();

                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });

        mSortRecyclerView.setLayoutManager(new LinearLayoutManager(_mActivity));
        mSortAdapter = new SortAdapter(R.layout.item_left_section, mTitles);
        mSortRecyclerView.setAdapter(mSortAdapter);
        mSortAdapter.setOnItemClickListener(this);
    }

    @Override
    public void getNaviData(List<NaviData> data) {
        mNaviData.clear();
        List<NaviSection> datas = new ArrayList<>();

        for (int i = 0; i < data.size(); i++) {
            NaviData datum =  data.get(i);
            NaviSection section = new NaviSection(true,datum.getName(),i);
            mTitles.add(datum.getName());
            datas.add(section);
            for (CollectBean bean : datum.getArticles()) {
                bean.setParentIndex(i);
                datas.add(new NaviSection(bean));
            }


        }

        mNaviAdapter.replaceData(datas);
        mSortAdapter.replaceData(mTitles);

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        mCurrentIndex = position;
        mSortAdapter.notifyDataSetChanged();
            mRecyclerView.smoothScrollToPosition(position);
        if (view.getId() == R.id.item_left_section_ly){
            mRecyclerView.smoothScrollToPosition(position);
        }
        int firstIndex = mManager.findFirstVisibleItemPosition();
        int lastIndex = mManager.findLastVisibleItemPosition();
        int middle = (lastIndex - firstIndex)/2;
        NaviSection section = mNaviAdapter.getItem(firstIndex);
        int index = 0;
        if (section != null) {
            if (section.t != null){
                index = section.t.getParentIndex();
            }else{
                index = section.getPosition();
            }
        }
        Lgger.d("zsr --> postition: "+middle+" "+position+" "+index);
        mRecyclerView.stopScroll();
        int topsize = mRecyclerView.getChildAt(position).getTop();
        Lgger.d("zsr --> : "+topsize);
        mRecyclerView.scrollBy(0,topsize);
        if (position == 0){
            //正上，负下
            mRecyclerView.scrollBy(0,-20);
        }
        if (position == 3){
            mRecyclerView.scrollBy(0,30);
        }
        if (position == 1){
            mRecyclerView.scrollToPosition(position);
        }
        if (position == 2){
            mRecyclerView.smoothScrollToPosition(position);
        }
    }


    class SortAdapter extends BaseQuickAdapter<String,BaseViewHolder>{


        public SortAdapter(int layoutResId, @Nullable List<String> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, String item) {
            helper.setText(R.id.item_left_section_tv,item);
            if (mCurrentIndex == helper.getLayoutPosition()){
               helper.setBackgroundColor(R.id.item_left_section_ly, ContextCompat.getColor(_mActivity,R.color.white));
            }else{
                helper.setBackgroundColor(R.id.item_left_section_ly, Color.TRANSPARENT);
            }
        }
    }


    @Override
    protected boolean showNormalView() {
        return false;
    }

    class DetailGoods{
        int parentIndex;
        List<Integer> childIndex = new ArrayList<>();


    }


}
